//
//  HMSubscribeTableViewController.m
//  HowToDo
//
//  Created by Apple on 16/12/31.
//  Copyright © 2016年 KY. All rights reserved.
//

#import "HMSubscribeTableViewController.h"
#import "HMRecommendTableViewController.h"
#import "HMSubscribeTitleView.h"
#import "HMFinishedSubscribeViewController.h"

#define PAGEINDEXVIEW_W 75
#define PAGEINDEXVIEW_H 3
#define TITLEVIEW_H 44

//  订阅
@interface HMSubscribeTableViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *subscribeView;
@property (nonatomic, strong) HMSubscribeTitleView *titleView;

@end

@implementation HMSubscribeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createSubscribeView];
    
    [self installTitleView];
    
    
    //初始界面为推荐
    self.subscribeView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titleView.pageIndexView.frame = CGRectMake(PAGEINDEXVIEW_W, TITLEVIEW_H - PAGEINDEXVIEW_H, PAGEINDEXVIEW_W, PAGEINDEXVIEW_H);
    
    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByOne];
    [super setupNavLeftItemsBySearch];
    //-------------设置导航栏结束--------------------------------
}

//设置导航栏
- (void)installTitleView
{
    HMSubscribeTitleView *titleView = [[HMSubscribeTitleView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
    
    self.titleView = titleView;
    
    titleView.btnBlock = ^(NSString *str)
    {
        if ([str isEqualToString:@"已订"]) {
            self.subscribeView.contentOffset = CGPointMake(0, 0);
            self.titleView.pageIndexView.frame = CGRectMake(0, TITLEVIEW_H - PAGEINDEXVIEW_H, PAGEINDEXVIEW_W, PAGEINDEXVIEW_H);
        }
        
        else
        {
            self.subscribeView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
            
            self.titleView.pageIndexView.frame = CGRectMake(PAGEINDEXVIEW_W, TITLEVIEW_H - PAGEINDEXVIEW_H, PAGEINDEXVIEW_W, PAGEINDEXVIEW_H);
        }
    };
    
    self.navigationItem.titleView = titleView;
}




//创建订阅窗口
- (void)createSubscribeView
{
    UIScrollView *subscribeView = [[UIScrollView alloc] init];
    
    self.subscribeView = subscribeView;
    self.subscribeView.delegate = self;
    
    self.subscribeView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 0);
    
    //弹簧,分页,滚动条
    self.subscribeView.bounces = NO;
    self.subscribeView.pagingEnabled = YES;
    self.subscribeView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:subscribeView];
//    self.view = subscribeView;
    [subscribeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(44);
        make.left.bottom.right.offset(0);
    }];
    
    //调用添加 "已订"和"推荐"
    [self createSubscribeFinish];
    [self createRecommendView];
}

//监听订阅的滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offset = self.subscribeView.contentOffset.x;
    
    NSLog(@"%f",offset);
    
    if (offset >= self.view.bounds.size.width)
    {
        self.titleView.pageIndexView.frame = CGRectMake(PAGEINDEXVIEW_W, TITLEVIEW_H - PAGEINDEXVIEW_H, PAGEINDEXVIEW_W, PAGEINDEXVIEW_H);
    }
    else
    {
        self.titleView.pageIndexView.frame = CGRectMake(0, TITLEVIEW_H - PAGEINDEXVIEW_H, PAGEINDEXVIEW_W, PAGEINDEXVIEW_H);
    }
}

//已订
- (void)createSubscribeFinish
{
    UIView *finishedview = [[UIView alloc] initWithFrame:self.view.bounds];

    HMFinishedSubscribeViewController *vc = [[HMFinishedSubscribeViewController alloc] init];
    [self addChildViewController:vc];
    
    [finishedview addSubview:vc.view];
    
    [self.subscribeView addSubview:finishedview];
    
}

//推荐窗口
- (void)createRecommendView
{
    UIView *recommendView = [[UIView alloc] init];
    
    recommendView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
    //创建推荐控制器
    HMRecommendTableViewController *vc = [[HMRecommendTableViewController alloc] init];
    
    [self addChildViewController:vc];
    
    //    将推荐控制器的窗口,赋值给推荐
    [recommendView addSubview:vc.view];
    
    [self.subscribeView addSubview:recommendView];
    
    
}

@end
